//
//  JSFormHeaderView.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/20.
//

import UIKit



class JSFormHeaderView : UITableViewHeaderFooterView {
    let FXFormHeaderViewID : String = "FXFormHeaderView"
    
    var  textClor : UIColor = UIColor(red: 123.0/255.0, green: 131.0/255.0, blue: 135.0/255.0, alpha: 1.0)
    var  textFont : UIFont = UIFont.systemFont(ofSize: 15.0)
    
    var  iconBtnImageWidth : CGFloat {
        return (self.iconBtn.currentImage?.size.width)!
    }
    var  iconBtnImageHeight : CGFloat {
        get{
            return (self.iconBtn.currentImage?.size.height)!
        }
    }
    var  cellMargin : CGFloat = 0
    var  model : JSFormModel = JSFormModel(){
        didSet{
            let iconImage:UIImage = (model.icon.count > 0 ? UIImage(named: model.icon):nil)!
            iconBtn.setImage(iconImage, for: .normal)
            iconBtn.setTitle(model.name, for: .normal)
        }
    }
    typealias clickBlock = (_ str:String)->Void
    var block : clickBlock?
    lazy var content:UILabel = {
        var content = UILabel()
        content.textColor = self.textClor
       
        return content
    }()

    lazy var container:UIView = {
        var container = UIView()
        container.backgroundColor = .systemGray6
        return container
    }()
    lazy var iconBtn:UIButton = {
        var _iconBtn = UIButton()
        _iconBtn.titleLabel!.font = self.textFont
        _iconBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 0)
        _iconBtn.contentHorizontalAlignment = .left
        _iconBtn.setTitleColor(self.textClor, for: .normal)
        return _iconBtn
    }()
    lazy var editBtn:UIButton = {
        var editBtn = UIButton()
        editBtn.setImage(UIImage(named: "shousuo"), for: .normal)//非折叠状体 收缩按钮
        editBtn.setImage(UIImage(named: "bianji"), for: .selected)//折叠时显示编辑
        editBtn.addTarget(self, action: #selector(click), for: .touchUpInside)
        
        return editBtn
    }()
    @objc func click(){
        block!("")
    }
    
    func widthWithFont(_ font:UIFont,constrainedToHeight height:CGFloat,text:String) -> CGFloat {
        let size:CGSize = CGSize(width: CGFloat(MAXFLOAT), height: height)
        let attr:Dictionary = [NSAttributedString.Key.font:font]
        let rect :CGRect = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attr, context: nil)
        return rect.size.width;
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .white
        contentView.addSubview(container)
        container.addSubview(iconBtn)
        container.addSubview(content)
        container.addSubview(editBtn)
        
       
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算iconBtn的总宽度：图片宽度+文字宽度+空隙的宽度
        let nameWidth:CGFloat = widthWithFont(self.textFont, constrainedToHeight: self.iconBtnImageHeight, text: self.model.name) + self.iconBtnImageWidth + 2 * 5
        
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0))
        }
        iconBtn.snp.remakeConstraints { (make) in
            make.left.equalTo(self.cellMargin)
            make.width.equalTo(nameWidth);
            make.centerY.equalToSuperview()
            make.height.equalTo(self.iconBtnImageHeight);
            
        }

        content.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconBtn.snp.right).offset(10)
            make.height.equalTo(30)
        }
        
        editBtn.snp.remakeConstraints { (make) in
            make.right.equalTo(-5)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(container.snp.height)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
}
